package org.example.stuview;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class TestView extends JFrame implements Runnable{
    static int num_topics = 5;
    static int test_paper;

    String stuID;
    String stuName;
    JPanel jPanelTest,jPanelSelect,jPanelImf;
    static JTextArea TopicArea;
    JButton submit,nextTopic,lastTopic;
    JRadioButton Select_A,Select_B,Select_C,Select_D ;
    JLabel jLabel = new JLabel();
    ButtonGroup bg;
    Timer timer = new Timer();
    TimerTask timerTask;

    static ArrayList<String> topics = new ArrayList<>(num_topics);
    ArrayList<String> isSelect = new ArrayList<>(num_topics);

    TestView(){
    }

    public void init(){
        //初始化list
        for (int i = 0; i < 5; i++) {
            isSelect.add("未知");
        }
        //
        this.setSize(1100,682);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
        this.setVisible(true);

        //设置时间
        timerTask =new TimerTask() {
            int i = 15;
            @Override
            public void run() {
                int hour = i % (24 * 60 * 60) / (60 * 60);
                int minute = i % (60 * 60) / 60;
                int second = i % 60;

                jLabel.setText("考试时间："+hour+"小时"+minute+"分"+second+"秒");
                i--;
                if (i < 0) {
                    timer.cancel();
                    jLabel.setText("Time Over");
                    submit.doClick();
                }
            }
        };
        timer.scheduleAtFixedRate(timerTask,0,1000);

        //考试界面
        jPanelTest = new JPanel();
        jPanelTest.setBackground(Color.BLACK);
        jPanelTest.setLayout(null);
        jPanelTest.setBounds(0,0,800,400);
        this.getContentPane().add(jPanelTest);
        //题目文本框
        Font topicFont = new Font(null,1,20);
        TopicArea = new JTextArea();
        TopicArea.setEditable(false);
        TopicArea.setFont(topicFont);
        TopicArea.setBounds(0,0,800,400);
        jPanelTest.add(TopicArea);

        //选择界面
        jPanelSelect = new JPanel();
        jPanelSelect.setBounds(0,400,800,250);
        jPanelSelect.setLayout(null);
        this.getContentPane().add(jPanelSelect);
        //选择按钮
        Font font = new Font(null,1,20);
        Select_A = new JRadioButton("A");
        Select_A.setFont(font);
        Select_B = new JRadioButton("B");
        Select_B.setFont(font);
        Select_C = new JRadioButton("C");
        Select_C.setFont(font);
        Select_D = new JRadioButton("D");
        Select_D.setFont(font);
        bg = new ButtonGroup();
        bg.add(Select_A);
        bg.add(Select_B);
        bg.add(Select_C);
        bg.add(Select_D);
        Select_A.setBounds(0,0,400,60);
        jPanelSelect.add(Select_A);
        Select_B.setBounds(400,0,400,60);
        jPanelSelect.add(Select_B);
        Select_C.setBounds(0,100,400,60);
        jPanelSelect.add(Select_C);
        Select_D.setBounds(400,100,400,60);
        jPanelSelect.add(Select_D);

        //信息界面
        jPanelImf = new JPanel();
        jPanelImf.setBounds(800,0,300,650);
        jPanelImf.setLayout(null);
        this.getContentPane().add(jPanelImf);
        //姓名
        JLabel nameTest = new JLabel("姓名：       " + stuName);
        nameTest.setFont(font);
        nameTest.setBounds(0,350,400,22);
        jPanelImf.add(nameTest);
        //ID
        JLabel IDText = new JLabel("ID：        " + stuID);
        IDText.setFont(font);
        IDText.setBounds(0,300,400,22);
        jPanelImf.add(IDText);
        //提交作业按钮
        submit = new JButton("交卷");
        submit.setBounds(140,550,100,35);
        jPanelImf.add(submit);
        //下一题按钮
        nextTopic = new JButton("下一题");
        nextTopic.setBounds(25,550,100,35);
        jPanelImf.add(nextTopic);
        //上一题按钮
        lastTopic = new JButton("上一题");
        lastTopic.setBounds(25,500,100,35);
        jPanelImf.add(lastTopic);
        //时间倒计时
        jLabel.setFont(font);
        jLabel.setBounds(0,425,400,35);
        jPanelImf.add(jLabel);
    }

    public void testListener(){
        HandleTest handleTest = new HandleTest();
        handleTest.setTestView(this);
        submit.addActionListener(handleTest);
        nextTopic.addActionListener(handleTest);
        lastTopic.addActionListener(handleTest);
    }

    @Override
    public void run() {
    }

}
